package com;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import com.care.vo.Comments;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.BeanUtils;

import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Array;
import java.util.*;

/**
 * @author yuhang
 * @desc
 * @since 2022/9/6
 */
public class ServiceCenter {

//    static String api = "http://127.0.0.1:8484/";

    static String api = "http://testservice.go2b2b.com/";

    public static void main(String[] args) throws Exception {

//            String result = addComments();


//        String result = getProductCommentProps();
//        String result = getProductCommentByProductId();


//        String result =checkProductComment();

//        String result = express();
//        String result=liveCategoryData();
        String result = xx();
//        String result=liveCategory();
        System.out.println(result);

    }

    private static String xx() throws Exception {
        String url = api + "/api/search/product/recommend/keywords";
         Map<String,Object> map =  new HashMap<>();
        return httpPost(url,map);
    }


    private static String liveCategory() throws Exception {
        String url = api + "/api/live/liveCategory";
        return httpPost(url,new HashMap<>());
    }

    private static String liveCategoryData() throws Exception {
        String url = api + "/api/live/liveCategoryData/0";
        return httpGet(url);
    }


    public static String getLogisticInfo() throws Exception {
        String url = "http://testexpress.go2b2b.com/api/getLogisticInfo";
        Map<String, Object> params = new HashMap<>();
        params.put("logisticCode", "462735051542270");
        return httpPost(url, params);
    }

    public static String express() throws Exception {
        String url = "http://carsexp.ximgs.net/api/getOrderExpress";
        Map<String, Object> params = new HashMap<>();
        params.put("shipperCode", "YTO");
        params.put("logisticCode", "YT8528337440460");
        params.put("customerName", "7087");
        return httpPost(url, params);
    }


    private static String checkProductComment() throws Exception {
        String url = api + "/api/comments/checkProductComment";
        Comments comments = new Comments(8L, "3,5,7", "9,10,11", 3283534L, 5, "");
        return httpPost(url, BeanUtil.beanToMap(comments));
    }


    private static String getProductCommentByProductId() throws Exception {
        String url = api + "/api/comments/getProductCommentByProductId";
        Comments comments = new Comments(8L, "3,5,7", "9,10,11", 3283534L, 5, "");
        return httpPost(url, BeanUtil.beanToMap(comments));
    }

    private static String getProductCommentProps() throws Exception {
        String url = api + "/api/comments/getProductCommentProps";
        Comments comments = new Comments(6L, "3,5,7", "9,10,11", 3L, 5, "");
        return httpPost(url, BeanUtil.beanToMap(comments));
    }


    private static String addComments() throws Exception {
        String url = api + "/api/comments/addProductComment";
        Comments comments = new Comments(8L, "3,4,5", "9", 3283534L, 5, "");
        return httpPost(url, BeanUtil.beanToMap(comments));

    }

    private static String addCommentsMuch() throws Exception {
        String url = api + "/api/comments/addProductComment";

        for (int i = 1000; i < 10001; i++) {

            String good = "1,2";
            String ser = "8,9";
            int socore = 5;
            if (i < 200 && i > 100) {
                good = "3,4";
                ser = "10,11";
                socore = 3;
            }
            if (i > 200 && i < 300) {
                good = "5";
                ser = "12";
                socore = 4;
            }

            if (i > 300 && i < 400) {
                good = "5,8";
                ser = "12,13";
                socore = 2;
            }

            if (i > 500 && i < 400) {
                good = "3,4,5";
                ser = "12,10,9";
                socore = 1;
            }

            int finalI = i;
            String finalGood = good;
            String finalSer = ser;
            int finalSocore = socore;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Comments comments = new Comments(Long.parseLong((finalI + 1) + ""), finalGood, finalSer, 3282154L, finalSocore, "");
                    try {
                        httpPost(url, BeanUtil.beanToMap(comments));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();


        }
        return null;
    }


    public static String httpPost(String url, Map<String, Object> params) throws Exception {
        String secret = "go2CN|20220119wearethebest";
        String nonce = UUID.randomUUID().toString();
        long timestamp = System.currentTimeMillis();
        String key = SecureUtil.sha1(secret + nonce + timestamp);
        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost post = new HttpPost(url);
        post.setHeader("sign", key);
        post.setHeader("nonce", nonce);
        post.setHeader("timestamp", timestamp + "");
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        Iterator<Map.Entry<String, Object>> iter = params.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry<String, Object> entry = iter.next();
            formparams
                    .add(new BasicNameValuePair(entry.getKey(), String.valueOf(entry.getValue())));

        }
        UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
        post.setEntity(uefEntity);
        String response = client.execute(post, new ResponseHandler<String>() {
            public String handleResponse(HttpResponse response) throws IOException {
                int status = response.getStatusLine().getStatusCode();
                if (status >= HttpStatus.SC_OK && status < HttpStatus.SC_MULTIPLE_CHOICES) {
                    HttpEntity entity = response.getEntity();
                    return entity != null ? EntityUtils.toString(entity, "utf-8") : null;
                } else {
                    System.out.println(response);
                }
                System.out.println(status);
                return null;
            }
        });
        return response;
    }



    public static String httpGet(String url) throws Exception {
        String secret = "go2CN|20220119wearethebest";
        String nonce = UUID.randomUUID().toString();
        long timestamp = System.currentTimeMillis();
        String key = SecureUtil.sha1(secret + nonce + timestamp);
        CloseableHttpClient client = HttpClients.createDefault();
        HttpGet post = new HttpGet(url);
        post.setHeader("sign", key);
        post.setHeader("nonce", nonce);
        post.setHeader("timestamp", timestamp + "");
        String response = client.execute(post, new ResponseHandler<String>() {
            @Override
            public String handleResponse(HttpResponse response) throws IOException {
                int status = response.getStatusLine().getStatusCode();
                if (status >= HttpStatus.SC_OK && status < HttpStatus.SC_MULTIPLE_CHOICES) {
                    HttpEntity entity = response.getEntity();
                    return entity != null ? EntityUtils.toString(entity, "utf-8") : null;
                } else {
                    System.out.println(response);
                }
                System.out.println(status);
                return null;
            }
        });
        return response;
    }
}
